package com.desire.controller;

import cn.hutool.core.lang.UUID;
import com.desire.common.DateUtil;
import com.desire.common.Result;
import com.desire.common.ResultUtil;
import com.desire.entity.BlogsEntity;
import com.desire.entity.UserEntity;
import com.desire.service.IBlogsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

import static com.desire.common.ResultEnum.*;

/**
 * (DBlogs)表控制层
 *
 * @author Desire
 * @since 2020-07-12 11:35:28
 */
@Api(tags = "博客操作接口")
@RestController
@RequestMapping("/blog")
public class BlogsController {
    /**
     * 服务对象
     */
    @Resource
    private IBlogsService blogsService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据id查询博客信息", notes = "根据id查询博客信息")
//    @ApiImplicitParam(name = "id", value = "博客id", required = true, dataType = "int", defaultValue = "1")
    @GetMapping("/{id}")
    public Result<Object> selectOne(@PathVariable Integer id) {
        BlogsEntity blogsEntity = blogsService.queryById(id);
        return ResultUtil.success(SUCCESS, blogsEntity);
    }

    /**
     * 查询总数据
     *
     * @return 总数据
     */
    @ApiOperation(value = "分页获取博客信息", notes = "分页获取博客信息")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "pageNum", value = "页数", required = true, dataType = "int", defaultValue = "1"),
//            @ApiImplicitParam(name = "pageSize", value = "每页数", required = true, dataType = "int", defaultValue = "5")
//    })
    @GetMapping("/{pageNum}/{pageSize}")
    public Result<Object> queryAll(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<BlogsEntity> blogs = this.blogsService.getBlogs();
        for (BlogsEntity blog : blogs) {
            blog.setReleaseTime(DateUtil.timeStamp2Date(blog.getReleaseTime(), null));
        }
        PageInfo<BlogsEntity> pageInfo = new PageInfo<>(blogs);
        return ResultUtil.success(SUCCESS, pageInfo);
    }

    /**
     * 添加博客
     *
     * @param blogsEntity
     * @return
     */
    @ApiOperation(value = "添加博客", notes = "添加博客")
    @PostMapping("/")
    public Result<Object> addBlog(BlogsEntity blogsEntity) {
        blogsEntity.setBlogNum(UUID.randomUUID() + DateUtil.timeStamp());
        blogsEntity.setReleaseTime(DateUtil.timeStamp());
        BlogsEntity blogs = this.blogsService.insert(blogsEntity);
        return ResultUtil.success(SUCCESS, blogs);
    }

    /**
     * 根据id删除博客
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除博客", notes = "根据id删除博客")
//    @ApiImplicitParam(name = "id", value = "博客id", required = true, dataType = "int", defaultValue = "1")
    @DeleteMapping("/{id}")
    public Result<Object> deleteBlogById(@PathVariable Integer id) {
        boolean result = this.blogsService.deleteById(id);
        if (result) {
            return ResultUtil.success(DELETE_SUCCESS, id);
        } else {
            return ResultUtil.error(DELETE_ERROR);
        }
    }

    @ApiOperation(value = "根据id修改博客", notes = "根据id修改博客")
    @PutMapping("/")
    public Result<Object> updateBlogById(BlogsEntity blogsEntity) {
        BlogsEntity blog = this.blogsService.update(blogsEntity);
        return ResultUtil.success(SUCCESS, blog);
    }
}